package com.loginlib.presenters;


import com.loginlib.panel.TablePanel;
import com.wanhe.base.model.entity.TableResultEntity;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.presents.BasePresenter;
import com.wanhe.base.runner.Runner;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.ViewUtil;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.callback.LoginCallBack;
import com.wanhe.base.websocket.callback.OpenTableCallBack;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2016/7/21.
 */
@Singleton
public class TablePresenter extends BasePresenter {

    public void setTablePanel(TablePanel tablePanel) {
        this.tablePanel = tablePanel;
    }

    private  TablePanel tablePanel;
    @Inject
    public TablePresenter(ServerApi api, Runner runner) {
        super(api, runner);

    }


    public void getTotalTableInfo(int pagId) {
        if(tablePanel!=null){
            tablePanel.onLoading();
        }
        runner.execute(api.GetTables(String.valueOf(pagId), "16"), new Consumer<TableResultEntity>() {
            @Override
            public void accept(@NonNull TableResultEntity resultEntity) throws Exception {
                if (ObjectUtil.checkObject(resultEntity)) {
                    if (resultEntity.resultType == TableResultEntity.SUCCESS) {
                        if(tablePanel!=null){
                            tablePanel.getTablesSuccess(resultEntity);
                        }
                    } else {
                        if(tablePanel!=null){
                            tablePanel.getTablesFail(resultEntity.resultMsg);
                        }
                    }
                }
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(runner!=null){
            runner.unsubscribe();
        }
    }



}
